layui.use(['table', 'admin', 'ax', 'func', 'form', 'layer'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var func = layui.func;
    var form = layui.form;
    var layer = layui.layer;

    //仓库移货编号
    var moveNum = $('#moveNum').val();
    var moveInWarehouse = Feng.getUrlParam('moveInWarehouse');
    var moveOutWarehouse = $('#moveOutWarehouse').val();
    var moveType = $('#moveType').val();

    /**
     * 商品（信息）二维码表管理
     */
    var WarehouseMoveProduct = {
        tableId: "warehouseMoveProductTable"
    };

    /**
     * 初始化表格的列
     */
    WarehouseMoveProduct.initColumn = function () {
        return [[
            // {type: 'checkbox'},
            {align: 'center', toolbar: '#tableBar', title: '操作', minWidth: 100},
            {field: 'id', hide: true, title: '主键'},
            {field: 'productId', hide: true},
            {field: 'productName', sort: true, title: '商品'},
            {field: 'supplierId', hide: true},
            {field: 'supplierName', sort: true, title: '供应商'},
            {field: 'shopId', hide: true},
            {field: 'shopName', sort: true, title: '分店'},
            {field: 'warehouseId', hide: true},
            {field: 'warehouseName', sort: true, title: '仓库', minWidth: 200},
            {field: 'shelfId', hide: true},
            {field: 'shelvesName', sort: true, title: '货架'},
            {field: 'saleUsableCount', sort: true, title: '销售可用库存'},
            {field: 'warehouseUsableCount', sort: true, title: '仓库可用库存'},

        ]];
    };

    /**
     * 点击查询按钮
     */
    WarehouseMoveProduct.search = function () {
        var queryData = {};

        queryData['productId'] = $('#productId').val();
        queryData['productName'] = $('#productName').val();

        table.reload(WarehouseMoveProduct.tableId, {
            where: queryData, page: {curr: 1}
        });
    };

    var index;

    /**
     * 点击编辑
     *
     * @param data 点击按钮时候的行数据
     */
    WarehouseMoveProduct.openEditDlg = function (obj, data) {
        //设置库存id
        $('#stockId').val(data.id);
        index = layer.open({
            //layer提供了5种层类型。可传入的值有：0（信息框，默认）1（页面层）2（iframe层）3（加载层）4（tips层）
            type: 1,
            title: "选择移入货架",
            area: ['500px', '300px'],
            content: $("#warehouseMoveProductDlg"), //引用的弹出层的页面层的方式加载修改界面表单
            success: function () {
                //在页面加载成功后，初始化 货架下拉框
                $.ajax({
                    url: '/baseCommonSelect/initShelfSelectOptions',
                    dataType: 'json',
                    data: {"warehouseId": moveInWarehouse},
                    type: 'post',
                    success: function (data) {
                        if (data !== null) {
                            //移入仓库
                            $("#moveInShelfId").empty();
                            $("#moveInShelfId").append(new Option());
                            $.each(data, function (index, item) {
                                $('#moveInShelfId').append(new Option(item.value, item.id));
                            });
                        } else {
                            $("#moveInShelfId").append(new Option("暂无数据", ""));
                        }
                        //重新渲染
                        form.render("select");
                    }
                });
            }
        });
    };


    //监听弹出框表单提交，massage是修改界面的表单数据'submit(btnSubmit),是修改按钮的绑定
    form.on('submit(btnSubmit)', function (dlgFormInfo) {
        /**
         *  发送请求后台 处理 该条修改的数据
         *  dlgFormInfo ： 表单数据
         */
        var moveInShelfName = $("#moveInShelfId option:selected").text();
        dlgFormInfo.field.moveInShelfName = moveInShelfName;
        dlgFormInfo.field.moveInWarehouse = Feng.getUrlParam('moveInWarehouse');
        dlgFormInfo.field.moveInWarehouseName = Feng.getUrlParam('moveInWarehouseName');
        dlgFormInfo.field.moveOutWarehouse = Feng.getUrlParam('moveOutWarehouse');
        dlgFormInfo.field.moveOutWarehouseName = Feng.getUrlParam('moveOutWarehouseName');
        dlgFormInfo.field.moveType = Feng.getUrlParam('moveType');

        //请求处理
        $.ajax({
            url: '/warehouseMove/addDealDetails',
            type: 'POST',
            data: dlgFormInfo.field,
            success: function (msg) {
                var returnCode = msg.code;//取得返回数据（Sting类型的字符串）的信息进行取值判断
                if (returnCode === 200) {
                    layer.alert("修改成功", {icon: 6}, function () {
                        layer.close(index);
                        layer.closeAll('dialog'); //关闭信息框
                        WarehouseMoveProduct.search();
                    });
                } else {
                    layer.msg("修改失败", {icon: 5});
                }
            }
        });
        return false;
    });

    var initQueryData = {};
    initQueryData['moveInWarehouse'] = Feng.getUrlParam('moveInWarehouse');
    initQueryData['moveOutWarehouse'] = Feng.getUrlParam('moveOutWarehouse');
    // 渲染表格
    var tableResult = table.render({
        elem: '#' + WarehouseMoveProduct.tableId,
        /**
         *该路径应为查询当前商品的库存信息 共通查询接口
         */
        url: Feng.ctxPath + '/baseProductStockSearch/listProductStockInfos',
        page: true,
        where: initQueryData,//传入查询参数
        height: 'full-200',
        limit: 10,
        cellMinWidth: 100,
        cols: WarehouseMoveProduct.initColumn()
    });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        WarehouseMoveProduct.search();
    });

    // 工具条点击事件
    table.on('tool(' + WarehouseMoveProduct.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'edit') {
            WarehouseMoveProduct.openEditDlg(obj, data);
        } else if (layEvent === 'delete') {
            WarehouseMoveProduct.onDeleteItem(data);
        }
    });
});
